package com.user.query.concurrent;

import org.apache.poi.ss.formula.functions.T;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * Created by ricky on 2017/3/17.
 *  延迟线程池
 *  信号量
 *  交换器
 * @author 华源润通郑州开发部
 * @version 1.1
 */
public class Scheduler {
    /**延迟执行任务*/
    private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor=new ScheduledThreadPoolExecutor(10);
    /**信号量*/
    private Semaphore semaphore;
    /**交换器*/
    private Exchanger<T> exchanger;

    /**只执行一次*/
    public  void  scheduler(Runnable event,long delay){
        //运行接口  延迟时间  延迟单位
        scheduledThreadPoolExecutor.schedule(event,delay, TimeUnit.MILLISECONDS);
    }
    /**循环执行*/
    public  void  schedulerRepeat(Runnable event,long delay,long period){
        //运行接口  初始延迟时间 第一次执行后的执行间隔周期  延迟单位
        scheduledThreadPoolExecutor.scheduleAtFixedRate(event,delay,period, TimeUnit.MILLISECONDS);
    }
    public  void usesemaphore(int size) throws InterruptedException {
        /*true 代表先进先出*/
        semaphore=new Semaphore(size,true);
        semaphore.acquire();/*获取许可*/
        semaphore.release();/*释放许可*/

    }
    /**交换对象  可在生产消费者的场景下使用*/
    public  void useexchanger(T holder) throws InterruptedException {
        exchanger.exchange(holder);
    }
}
